<template>
  <div>
    <div class="">
      <el-button type="primary" @click="executeAll">全部执行</el-button>
    </div>
    <el-table :data="list" border style="width: 100%;margin-top: 15px;">
      <el-table-column prop="name" label="任务名"></el-table-column>
      <el-table-column prop="dir" label="项目根目录"></el-table-column>
      <el-table-column
        prop="buildCommond"
        label="打包命令"
      ></el-table-column>
      <el-table-column
        prop="otherCommond"
        label="其他命令"
      ></el-table-column>
      <el-table-column prop="status" label="状态">
        <template #default="scope">
          {{ statusList?.[scope.row.status] || "" }}
        </template>
      </el-table-column>
      <el-table-column label="服务器配置" width="400">
        <template #default="scope">
          <div v-for="(value, key) in scope.row">
            {{ key }}: {{ value }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button link type="primary" @click="start(scope.row)">开始</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script setup>
import { ref } from "vue";
const props = defineProps({
  data: Object,
});
const dialogVisible = ref(false);
const dialogTitle = ref('');
const formStatus = ref(0);
const list = ref([]);
const formData = ref({});
const statusList = ref(["平常状态", "待执行", "执行中等待中", "执行中", "执行完成"])

const emit = defineEmits(["updateConfig"]);
// 获取待执行任务列表
const init = async () => {
  const result = await window.pywebview.api.getJobListByStatus("1");
  list.value = result
}
init()
// 开始操作
const start = async (item) => {
  ElMessageBox.confirm(
    '确认开始执行任务吗？',
    '提示',
    {
      distinguishCancelAndClose: true,
      confirmButtonText: '确认',
      cancelButtonText: '取消',
    }
  )
    .then(async() => {
      try {
        window.pywebview.api.startJob([item.id]);
        ElMessage.success('开始执行成功')
        init()
      } catch (error) {
        ElMessage.error(error.message)
      }
    })
    .catch(() => {
    })
};

// 全部执行
const executeAll = () => {
  ElMessageBox.confirm(
    '确认全部执行任务吗？',
    '提示',
    {
      distinguishCancelAndClose: true,
      confirmButtonText: '确认',
      cancelButtonText: '取消',
    }
  )
    .then(async() => {
      try {
        window.pywebview.api.startJob(list.value.map(item => item.id));
        ElMessage.success('执行中')
        init()
      } catch (error) {
        ElMessage.error(error.message)
      }
    })
    .catch(() => {
    })
}

// 将方法暴露给父组件
defineExpose({
  init
});
</script>

